// miniprogram/pages/index.js
const db = wx.cloud.database(); //初始化数据库
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		shopList: 0,
		limit: 0,
		// 店铺列表
		shopsList: [],
		// 分类图标
		classify: [{
				icon: 'https://7461-take-out-9gg0dhd7c2f86b6b-1304355002.tcb.qcloud.la/index/images/%E9%A5%AD%20.png?sign=c62bcb359f1e095c825f8bd8a46531c8&t=1613910722',
				title: '经典饭食'
			},
			{
				icon: 'https://7461-take-out-9gg0dhd7c2f86b6b-1304355002.tcb.qcloud.la/index/images/%E9%9D%A2%E9%A3%9F.png?sign=18c0b927f381fcc936b4ce9c741d459c&t=1613910696',
				title: '高汤粉面'
			},
			{
				icon: 'https://7461-take-out-9gg0dhd7c2f86b6b-1304355002.tcb.qcloud.la/index/images/%E9%A5%AE%E6%96%99-%E5%A5%B6%E8%8C%B6.png?sign=af2172b9536b7ccdde46151469d8014e&t=1613910737',
				title: '奶茶甜品'
			},
			{
				icon: 'https://7461-take-out-9gg0dhd7c2f86b6b-1304355002.tcb.qcloud.la/index/images/%E6%B1%89%E5%A0%A1.png?sign=ee362061b544c42a54e8a937e4983e7a&t=1613910747',
				title: '小吃炸鸡'
			},
			{
				icon: 'https://7461-take-out-9gg0dhd7c2f86b6b-1304355002.tcb.qcloud.la/index/images/%E5%94%AE%E5%90%8E.png?sign=681a30561c6a4540e17e91c435b7335a&t=1613910759',
				title: '售后客服'
			}
		],
		// 轮播图
		background: [{
				url: 'https://7461-take-out-9gg0dhd7c2f86b6b-1304355002.tcb.qcloud.la/index/images/%E4%B8%8B%E5%8D%95%E5%BF%85%E7%9C%8B.png?sign=7e6cee22e9ea483568fd5dfbb73d1c35&t=1620568823'
			},
			{
				url: 'https://7461-take-out-9gg0dhd7c2f86b6b-1304355002.tcb.qcloud.la/index/images/%E5%8F%8C%E5%8D%81%E4%B8%80.png?sign=6b1b063d3388d82b985ace351933775d&t=1620568847'
			},
			{
				url: 'https://7461-take-out-9gg0dhd7c2f86b6b-1304355002.tcb.qcloud.la/index/images/%E6%96%B0%E4%BA%BA%E6%B3%A8%E5%86%8C.png?sign=aec92e2b4e6ab6621a5a9acf6a67f7b9&t=1613910794'
			}
		]
	},
	// 跳转搜索页
	gotoSearch: function() {
		wx.navigateTo({
			url: '../search/search',
		});
	},
	// 获取商店
	getShopList: function() {
		db.collection('shop').get()
			.then(res => {
				// console.log(res.data)
				this.setData({
					shopsList: res.data,
					limit: 20
				})
			})
		console.log(this.data.shopsList)
	},
	// 跳转商店
	gotoShop: function(event) {
		// console.log(event.currentTarget.dataset.shopid)
		wx.reLaunch({
			url: `../shop/shop?shopid=${event.currentTarget.dataset.shopid}`,
		})
	},
	// 跳转分类
	gotoClass: function(e) {
		wx.navigateTo({
			url: `../classify/classify?type=${e.currentTarget.dataset.type}`,
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.getShopList()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		this.getTabBar().init();
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		db.collection('shop').skip(this.data.limit).limit(10).get()
			.then(res => {
				wx.showToast({
					title: '加载中',
					icon: 'loading',
					duration: 500
				})
				// console.log(res.data)
				this.setData({
					shopsList: this.data.shopsList.concat(res.data),
					limit: this.data.limit + 10
				})
			})
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
